package com.cloudwise.truelicensespringbootstarter.license.model;

import de.schlichtherle.license.AbstractKeyStoreParam;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 自定义keyStoreParam，将公钥文件存在指定位置
 * @author jeffery.li
 * @date 2022/6/8 16:48
 */
public class CustomKeyStoreParam extends AbstractKeyStoreParam {


    private String storePath;

    private String alias;

    private String storePwd;

    private String keyPwd;

    /**
     * Creates a new instance of AbstractKeyStoreParam which will look up
     * the given resource using the class loader of the given class when
     * calling {@link #getStream()}.
     *
     * @param clazz    the class which refers to the class loader to use.
     * @param resource the resource to look up.
     */
    public CustomKeyStoreParam(Class clazz, String resource, String alias, String storePwd, String keyPwd) {

        super(clazz, resource);
        this.storePath = resource;
        this.alias = alias;
        this.storePwd = storePwd;
        this.keyPwd = keyPwd;
    }


    @Override
    public String getAlias() {
        return alias;
    }

    @Override
    public String getStorePwd() {
        return storePwd;
    }

    @Override
    public String getKeyPwd() {
        return keyPwd;
    }

    /**
     * AbstractKeyStoreParam里面的getStream()方法默认文件是存储的项目中。
     * 用于将公私钥存储文件存放到其他磁盘位置而不是项目中
     */
    @Override
    public InputStream getStream() throws IOException {
        return new FileInputStream(new File(storePath));
    }
}
